//
//  BVSearch.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/25.
//  Copyright © 2016年 com.yinding. All rights reserved.
//  

#import "BVSearch.h"
#define HISTORY_SEARCH @"historys"

@interface BVSearch ()

@property (nonatomic, strong) NSUserDefaults *userDefaults;

@end

@implementation BVSearch

- (void)saveSearchText:(NSString *)searchText {

    NSArray *history = [self.userDefaults arrayForKey:HISTORY_SEARCH];
    NSMutableArray *historyArr = [NSMutableArray arrayWithArray:history];
    
    if (![historyArr containsObject:searchText]) {
        [historyArr insertObject:searchText atIndex:0];
    }
    [self.userDefaults setObject:historyArr forKey:HISTORY_SEARCH];
}

- (NSMutableArray *)fetchSearchText {
    NSArray *history = [self.userDefaults objectForKey:HISTORY_SEARCH];
    NSMutableArray *historyArr = [NSMutableArray arrayWithArray:history];
    return historyArr;
}

- (void)deleteAllSearchText {
    [self.userDefaults removeObjectForKey:HISTORY_SEARCH];
}

- (NSUserDefaults *)userDefaults {
    if (!_userDefaults) {
        _userDefaults = [NSUserDefaults standardUserDefaults];
    }
    return _userDefaults;
}


@end
